package com.myapp.ruyixing.ui.shopmanager;

import static android.widget.Toast.LENGTH_SHORT;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.myapp.ruyixing.R;
import com.myapp.ruyixing.http.RYHttpClient;
import com.myapp.ruyixing.http.RYHttpResponseHandler;
import com.myapp.ruyixing.model.ShopInfo;
import com.myapp.ruyixing.model.ShopInfo.InfoItem;
import com.myapp.ruyixing.ui.BaseActivity;
import com.myapp.ruyixing.ui.ShopDetailActivity;
import com.myapp.ruyixing.ui.widget.TitleBar;
import com.myapp.ruyixing.utils.BitmapLruCache;
import com.myapp.ruyixing.utils.LoginHelper;

// 店铺管理
public class ShopManageActivity extends BaseActivity implements OnClickListener {

	private TitleBar mTitleBar;
	private View mLoadingView;

	private TextView mTitleView;
	private NetworkImageView mImageView;
	private ImageView mScoreView;
	private TextView mDistanceView;
	private TextView mKeywordView;
	private TextView mPriceView;

	private TextView mAddressView;
	private TextView mPhoneView;
	private TextView mOpenTimeView;
	private TextView mCharacterView;
	private TextView mEnvironmentView;
	private TextView mOverviewView;

	private ShopInfo mShopInfo;

	private void loadViews(ShopInfo shopInfo) {
		if (shopInfo != null) {
			findViewById(R.id.layout_content).setVisibility(View.VISIBLE);
			mTitleView.setText(shopInfo.shopname);
			RequestQueue mQueue = Volley
					.newRequestQueue(ShopManageActivity.this);
			ImageLoader mImageLoader = new ImageLoader(mQueue,
					new BitmapLruCache());
			mImageView.setImageUrl(shopInfo.picurl, mImageLoader);
			mScoreView.setImageLevel(shopInfo.score);
			mPriceView.setText("人均：￥" + shopInfo.price);
			mKeywordView.setText("卫生：" + shopInfo.weisheng + "    服务："
					+ shopInfo.fuwu);

			if (shopInfo.items != null) {
				for (int i = 0; i < shopInfo.items.size(); i++) {
					InfoItem item = shopInfo.items.get(i);
					if (item != null) {
						if ("address".equals(item.field)) {
							mAddressView.setVisibility(View.VISIBLE);
							mAddressView.setText("地址：" + item.content);
						} else if ("mobile".equals(item.field)) {
							mPhoneView.setVisibility(View.VISIBLE);
							mPhoneView.setText("电话：" + item.content);
						} else if ("openhour".equals(item.field)) {
							findViewById(R.id.layout_open_time).setVisibility(
									View.VISIBLE);
							mOpenTimeView.setText(item.content);
						} else if ("description".equals(item.field)) {
							findViewById(R.id.layout_overview).setVisibility(
									View.VISIBLE);
							mOverviewView.setText(item.content);
						}
					}
				}
			}
		}
	}

	private RYHttpResponseHandler mHttpResponseHandler = new RYHttpResponseHandler() {

		@Override
		public void onSuccess(Object o) {
			try {
				ShopInfo shopInfo = (ShopInfo) o;
				mShopInfo = shopInfo;
				onLoadingDataFinished(true, shopInfo);
			} catch (Exception e) {
				onLoadingDataFinished(false, null);
			}
		}

		@Override
		public void onFailure(Throwable throwable) {
			onLoadingDataFinished(false, null);
		}
	};

	private void onLoadingDataStarted(String shopid, String shopbduss) {
		showLoading();
		RYHttpClient.getShopInfo(this, mHttpResponseHandler, shopid, shopbduss);
	}

	private void onLoadingDataFinished(boolean succeed, ShopInfo shopInfo) {
		if (succeed && shopInfo != null) {
			loadViews(shopInfo);
		} else {
			Toast.makeText(this, "加载商户失败！", LENGTH_SHORT).show();
		}
		hideLoading();
	}

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);

		setContentView(R.layout.activity_shop_manager_shop_manage);

		initView();

		onLoadingDataStarted(LoginHelper.getInstance().getShopId(), LoginHelper
				.getInstance().getShopBduss());
	}

	private void initView() {
		mTitleBar = (TitleBar) findViewById(R.id.title_bar);
		mLoadingView = findViewById(R.id.my_loading);

		mTitleBar.setOnIconClickedListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				onBackPressed();
			}
		});

		mTitleView = (TextView) findViewById(R.id.title);
		mImageView = (NetworkImageView) findViewById(R.id.image);
		mScoreView = (ImageView) findViewById(R.id.score);
		mPriceView = (TextView) findViewById(R.id.price);
		mDistanceView = (TextView) findViewById(R.id.distance);
		mKeywordView = (TextView) findViewById(R.id.keywords);

		mAddressView = (TextView) findViewById(R.id.address);
		mPhoneView = (TextView) findViewById(R.id.phone);
		mOpenTimeView = (TextView) findViewById(R.id.open_time);
		mOverviewView = (TextView) findViewById(R.id.overview);
		mCharacterView = (TextView) findViewById(R.id.character);
		mEnvironmentView = (TextView) findViewById(R.id.environment);

		mAddressView.setOnClickListener(this);
		mPhoneView.setOnClickListener(this);
		mImageView.setOnClickListener(this);
		findViewById(R.id.layout_open_time).setOnClickListener(this);
		findViewById(R.id.layout_character).setOnClickListener(this);
		findViewById(R.id.layout_environment).setOnClickListener(this);
		findViewById(R.id.layout_overview).setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		Intent i = new Intent(this, ShopEditActivity.class);
		switch (v.getId()) {
		case R.id.address:
			i.putExtra("name", "地址");
			i.putExtra("field", "address");

			break;
		case R.id.phone:
			i.putExtra("name", "电话");
			i.putExtra("field", "mobile");

			break;
		case R.id.layout_open_time:
			i.putExtra("name", "营业时间");
			i.putExtra("field", "openhour");

			break;
		case R.id.layout_character:
			i.putExtra("name", "餐厅特色");

			break;
		case R.id.layout_environment:
			i.putExtra("name", "餐厅氛围");

			break;
		case R.id.layout_overview:
			i.putExtra("name", "简介");
			i.putExtra("field", "description");

			break;
		case R.id.image:
			i = new Intent(this, ShopAlbumForManagerActivity.class);
			i.putExtra(ShopDetailActivity.INTENT_SHOP_ID, LoginHelper
					.getInstance().getShopId());
			i.putExtra(ShopDetailActivity.INTENT_SHOP_NAME, LoginHelper
					.getInstance().getShopName());

			break;

		default:
			i = null;
			break;
		}

		if (i != null) {
			startActivityForResult(i, 1);
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode,
			Intent intent) {
		if (requestCode == 1 && resultCode == RESULT_OK && intent != null) {
			String field = intent.getStringExtra("field");
			String content = intent.getStringExtra("content");
			if (!TextUtils.isEmpty(field) && !TextUtils.isEmpty(content)) {

				if ("address".equals(field)) {
					mAddressView.setVisibility(View.VISIBLE);
					mAddressView.setText("地址：" + content);
				} else if ("mobile".equals(field)) {
					mPhoneView.setVisibility(View.VISIBLE);
					mPhoneView.setText("电话：" + content);
				} else if ("openhour".equals(field)) {
					findViewById(R.id.layout_open_time).setVisibility(
							View.VISIBLE);
					mOpenTimeView.setText(content);
				} else if ("description".equals(field)) {
					findViewById(R.id.layout_overview).setVisibility(
							View.VISIBLE);
					mOverviewView.setText(content);
				}
			}
		}
		super.onActivityResult(requestCode, resultCode, intent);
	}

	private void showTitleBarLoading() {
		mTitleBar.showLoading(true);
	}

	private void hideTitleBarLoading() {
		mTitleBar.showLoading(false);
	}

	private void showLoading() {
		mLoadingView.setVisibility(View.VISIBLE);
	}

	private void hideLoading() {
		mLoadingView.setVisibility(View.GONE);
	}
}
